package com.example.mr5.money.main.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.mr5.money.R;

public class Calculator extends AppCompatActivity {

    View vi;
    String str = "", secstr = "";
    EditText primaryET;
    /* EditText secondaryET; */
    int flag = 0, c = 0;
    double b = 0.0, g = 0.0, f = 0.0;

    // 对输出的数进行规范化
    public void substr() {
        int a = str.indexOf(".");
        int b = str.indexOf("E");
        String tempstr = str;
        if (a == -1) {
            if(str.length()>12)
                str = str.substring(0, 12);
        }
        if (a > 0) {
            if (b == -1) {
                if(str.length()>12)
                    str = str.substring(0, 12);
            }
            if (b > 0) {
                tempstr = str.substring(b);
                if (str.length() > 12) {
                    int perStrLen = 12 - tempstr.length();
                    str = str.substring(0, perStrLen) + tempstr;
                }
            }
        }

    }

    // 计算方法
    public double calc() {
        switch (c) {
            case 0:
                f = g;
                break;
            case 1:
                f = b + g;
                break;
            case 2:
                f = b - g;
                break;
            case 3:
                f = b * g;
                break;
            case 4:
                f = b / g;
                break;
        }

        b = f;
        c = 0;

        return f;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        setTitle(getResources().getString(R.string.calculate));
        android.support.v7.app.ActionBar bar = getSupportActionBar();
        bar.setDisplayHomeAsUpEnabled(true);

        // 获取所有按键，保存到数组当中去
        final Button number[] = new Button[10];
        final Button mark[] = new Button[13];

        // 获取功能按钮
        mark[0] = (Button) findViewById(R.id.markone);
        mark[1] = (Button) findViewById(R.id.marktwo);
        mark[2] = (Button) findViewById(R.id.markthree);
        mark[3] = (Button) findViewById(R.id.markfour);
        mark[4] = (Button) findViewById(R.id.markfive);
        mark[5] = (Button) findViewById(R.id.marksix);
        mark[6] = (Button) findViewById(R.id.markseven);
        mark[7] = (Button) findViewById(R.id.markeight);
        mark[8] = (Button) findViewById(R.id.marknine);
        mark[9] = (Button) findViewById(R.id.markten);
        mark[10] = (Button) findViewById(R.id.markeleven);
        mark[11] = (Button) findViewById(R.id.marktwelve);
        mark[12] = (Button) findViewById(R.id.markthreeteen);

        // 获取数字按钮
        number[0] = (Button) findViewById(R.id.btnzero);
        number[1] = (Button) findViewById(R.id.btnone);
        number[2] = (Button) findViewById(R.id.btntwo);
        number[3] = (Button) findViewById(R.id.btnthree);
        number[4] = (Button) findViewById(R.id.btnfour);
        number[5] = (Button) findViewById(R.id.btnfive);
        number[6] = (Button) findViewById(R.id.btnsix);
        number[7] = (Button) findViewById(R.id.btnseven);
        number[8] = (Button) findViewById(R.id.btneight);
        number[9] = (Button) findViewById(R.id.btnnine);

        // 获取主显示屏
        primaryET = (EditText) findViewById(R.id.edittext2_show);

		/*
		 * // 获取副显示屏 secondaryET = (EditText) findViewById(R.id.edittext1_show);
		 */

        primaryET.setText(str);
		/* secondaryET.setText(secstr); */

        // 对C按钮的事件的处理
        mark[0].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                b = 0.0;
                c = 0;
                g = 0.0;
                str = "";
                primaryET.setText(str);
                secstr = "";
				/* secondaryET.setText(secstr); */
            }
        });

        // 对CE按钮的事件的处理
        mark[1].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                str = "";
                primaryET.setText(str);
                vi = v;
            }
        });

        // 对平方的事件的处理
        mark[2].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "") {
                    double temp = Double.parseDouble(str);
                    str = "" + temp * temp;
                    substr();
                    primaryET.setText(str);
                }
            }
        });

        // 对根号的事件的处理
        mark[3].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "") {
                    double temp = Double.parseDouble(str);
                    str = Math.sqrt(temp) + "";
                    substr();
                    primaryET.setText(str);
                }
            }
        });
        // 对backspace的事件的处理
        mark[4].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "") {
                    if(str.length()==1){
                        str="";
                        primaryET.setText(str);
                    }
                    else{
                        str = str.substring(0, str.length() - 1);
                        primaryET.setText(str);
                    }

                }

                else {
                    primaryET.setText(str);
                }
            }
        });

        // 对正负号的事件的处理
        mark[5].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (vi != mark[11] && str != "") {
                    char ch = str.charAt(0);
                    if (ch == '-')
                        str = str.replace("-", "");
                    else
                        str = "-" + str;
                    primaryET.setText(str);
                }
            }
        });

        // 对求1/x事件的处理
        mark[6].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "") {
                    Double d = Double.parseDouble(str);
                    Double s = 1.0 / d;
                    str = "" + s.toString();
                    substr();
                    primaryET.setText(str);
                } else {
                    primaryET.setText("0");
                }
            }
        });

        // 对除号的事件的处理
        mark[7].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "") {
                    if (vi == mark[7] || vi == mark[8] || vi == mark[9]
                            || vi == mark[10]) {
                        c = 4;
                    } else {
                        g = Double.parseDouble(str);
                        calc();
                        str = "" + f;
                        substr();
                        primaryET.setText(str);
                        c = 4;
                        flag = 1;
                        vi = v;
                    }
                }
            }
        });

        // 对乘号的事件的处理
        mark[8].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "") {
                    if (vi == mark[7] || vi == mark[8] || vi == mark[9]
                            || vi == mark[10]) {
                        c = 3;
                    } else {
                        g = Double.parseDouble(str);
                        calc();
                        str = "" + f;
                        substr();
                        primaryET.setText(str);
                        c = 3;
                        flag = 1;
                        vi = v;
                    }
                }
            }
        });

        // 对减号的事件的处理
        mark[9].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "") {
                    if (vi == mark[7] || vi == mark[8] || vi == mark[9]
                            || vi == mark[10]) {
                        c = 2;
                    } else {
                        g = Double.parseDouble(str);
                        calc();
                        str = "" + f;
                        substr();
                        primaryET.setText(str);
                        c = 2;
                        flag = 1;
                        vi = v;
                    }
                }
            }
        });

        // 对加号的事件的处理
        mark[10].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "") {
                    if (vi == mark[7] || vi == mark[8] || vi == mark[9]
                            || vi == mark[10]) {
                        c = 1;
                    } else {
                        g = Double.parseDouble(str);
                        calc();
                        str = "" + f;
                        substr();
                        primaryET.setText(str);
                        c = 1;
                        flag = 1;
                        vi = v;
                    }
                }
            }
        });

        // 对小数点的处理
        mark[11].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str == "") {
                    str += ".";
                    primaryET.setText(str);
                } else {
                    char ch[];
                    int x = 0;
                    ch = str.toCharArray();
                    for (int i = 0; i < ch.length; i++)
                        if (ch[i] == '.')
                            x++;
                    if (x == 0) {
                        str += ".";
                        primaryET.setText(str);
                    }

                }
            }
        });

        // 对等号的事件的处理
        mark[12].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (str != "" && vi != mark[7] && vi != mark[8]
                        && vi != mark[9] && vi != mark[10]) {
                    g = Double.parseDouble(str);
                    calc();
                    str = "" + f;
                    substr();
                    primaryET.setText(str);
                    flag = 1;
                    vi = v;

                }
            }
        });
        // 设定数字按键
        number[0].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 0;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    char ch1[];
                    ch1 = str.toCharArray();
                    if (!(ch1.length == 1 && ch1[0] == '0')) {
                        str += 0;
                        primaryET.setText(str);
                    }

                }
                vi = v;
            }
        });

        number[1].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 1;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 1;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

        number[2].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 2;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 2;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

        number[3].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 3;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 3;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

        number[4].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 4;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 4;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

        number[5].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 5;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 5;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

        number[6].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 6;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 6;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

        number[7].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 7;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 7;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

        number[8].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 8;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 8;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

        number[9].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (flag == 1) {
                    str = "";
                    str += 9;
                    primaryET.setText(str);
                    flag = 0;
                } else {
                    str += 9;
                    primaryET.setText(str);
                }
                vi = v;
            }
        });

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //菜单栏返回键功能
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
